﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessHandler;
using EasyCode.Utility;
using BusinessHanler;
public partial class Page_UserAccount : System.Web.UI.Page
{
    private AccountController accountController;
    private int currentPage = 1;
    private int pageSize = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.LoadList();
        }
    }
    protected void Button_Search_Click(object sender, EventArgs e)
    {
        this.LoadList();
    }
    protected void Button_Cancel_Click(object sender, EventArgs e)
    {
        this.TextBox_UserName.Text = string.Empty;
        this.DropDownList_Ban.SelectedIndex = 0;
        this.DropDownList_Status.SelectedIndex = 0;
        this.LoadList();
    }
    protected void Repeater_Content_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e != null)
        {
            string commandName = e.CommandName;
            int id = Protector.Int(e.CommandArgument, 0);
            if (id > 0)
            {
                accountController = AccountController.Instance;
                AccountUser account = (from o in accountController.AccountUsers where o.AccountUserID == id select o).SingleOrDefault();
                if (account != null)
                {                   
                    if (commandName == "changebanstatus")
                    {
                        if (account.AccountBanned == null || account.AccountBanned == false)
                        {
                            account.AccountBanned = true;
                        }
                        else
                        {
                            account.AccountBanned = false;
                        }
                        try
                        {
                            accountController.SubmitChanges();
                            this.Message_Alert.SetSuccess(Resources.UserAccount.ChangeBanStatus);
                            this.LoadList();
                        }
                        catch (Exception ex)
                        {
                            this.Message_Alert.SetError(Resources.UserAccount.CannotChanBanStatus + " Error: " + ex.Message);
                        }
                    }
                    else if (commandName == "LockUnLockCommand")
                    {
                        if (account.AccountUserStatus == null || account.AccountUserStatus == false)
                        {
                            account.AccountUserStatus = true;
                        }
                        else
                        {
                            account.AccountUserStatus = false;
                        }
                        try
                        {
                            accountController.SubmitChanges();
                            this.Message_Alert.SetSuccess(Resources.UserAccount.UpdateStatusSuccess);
                            this.LoadList();
                        }
                        catch (Exception ex)
                        {
                            this.Message_Alert.SetError(Resources.UserAccount.UpdateStatusFail + " Error: " + ex.Message);
                        }
                    }
                    //AccountController.Instance = new AccountController();
                }
            }
        }
    }
    protected void UpdatePanelContent_Load(object sender, EventArgs e)
    {
        string arg = Request.Form["__EVENTARGUMENT"];
        if (string.IsNullOrEmpty(arg)) return;
        if (arg != null)
        {
            string[] temp = arg.Split('|');
            if (temp.Length == 2)
            {
                if (temp[1].ToString().ToUpper() == "PAGE")
                {
                    this.currentPage = Protector.Int(temp[0].ToString(), 0);
                    this.LoadList();
                }
            }
        }
    }
    private void LoadList()
    {
        //AccountController.Instance = null;
        accountController = AccountController.Instance;
        int skip = (currentPage - 1) * pageSize;
        int total = 0;
        AccountUser condition = new AccountUser();
        if (!string.IsNullOrEmpty(this.TextBox_UserName.Text.Trim()))
        {
            condition.AccountUserName = this.TextBox_UserName.Text.Trim();
        }
        if (this.DropDownList_Status.SelectedIndex > 0)
        {
            condition.AccountUserStatus = false;
            if (this.DropDownList_Status.SelectedValue.ToString() == "1")
            {
                condition.AccountUserStatus = true;
            }
        }
        if (this.DropDownList_Ban.SelectedIndex > 0)
        {
            condition.AccountBanned = false;
            if (DropDownList_Ban.SelectedValue.ToString() == "1")
            {
                condition.AccountBanned = true;
            }
        }
        this.Repeater_Content.DataSource = accountController.GetList(condition, skip, pageSize, ref total);
        this.Repeater_Content.DataBind();
        if (total > 0)
        {
            tr_not_found.Visible = false;
        }
        else
        {
            tr_not_found.Visible = true;
        }
        this.BuildPageList(total);
    }
    private void BuildPageList(int numOfRecord)
    {
        string pagingText = "";
        int numOfPage = (numOfRecord / pageSize);
        if ((numOfRecord % pageSize) > 0)
        {
            numOfPage = numOfPage + 1;
        }
        if (numOfPage > 1)
        {
            string pagingLink = Commons.LinkPageForAdmin(numOfPage, this.currentPage, 11, 3);
            pagingText = "<div class='paginator2 right'>" + pagingLink + "</div>";
        }
        Literal_PagingListBot.Text = Literal_PagingListTop.Text = pagingText;
    }


}